package com.cq.hd.app.controller;

import com.cq.hd.common.response.Result;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.system.api.TbRegionsApi;
import com.cq.hd.system.api.vo.RegionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 地区(es_regions) 前端控制器
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-22
 */
@Api(tags = "地区相关接口")
@Slf4j
@RestController
@RequestMapping("/regions")
public class RegionsController {

    private TbRegionsApi regionsApi;

    @Autowired
    public void setRegionsApi(TbRegionsApi regionsApi) {
        this.regionsApi = regionsApi;
    }

    @ApiOperation(value = "根据id获取省市列表数据")
    @ApiImplicitParam(name = "id", value = "id:0：获取省份，其他id：获取市区数据", defaultValue = "0", paramType = "query", dataType = "Integer")
    @ApiResponse(response = RegionVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listRegion")
    public Result<List<RegionVo>> listRegion(Integer id) {
        return regionsApi.listRegion(id);
    }

    @ApiOperation(value = "获取省市列表所有数据")
    @ApiImplicitParam(name = "cityName", value = "市区名称", paramType = "query", dataType = "String")
    @ApiResponse(response = RegionVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listAllRegion")
    public Result<List<RegionVo>> listAllRegion(String cityName) {
        // 去除空格，换行等
        if (!StringUtils.isBlank(cityName)) {
            cityName = cityName.replaceAll(" ", "").replaceAll("[\\t\\n\\r]", "");
        } else {
            cityName = "";
        }
        return regionsApi.listAllRegion(cityName);
    }

}
